package com.aliengen.thingple.driverapp.ui.activities;

import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.aliengen.thingple.driverapp.R;
import com.aliengen.thingple.driverapp.data.exception.InvalidTagException;
import com.aliengen.thingple.driverapp.data.local.TruckModel;
import com.aliengen.thingple.driverapp.data.models.Tag;
import com.aliengen.thingple.driverapp.data.models.TruckTag;
import com.aliengen.thingple.driverapp.network.api.ThingpleAPI;
import com.aliengen.thingple.driverapp.network.api.exception.APICallException;
import com.aliengen.thingple.driverapp.ui.fragments.DialogRFIDReadingFragment;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.OptionsMenu;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
@EActivity(R.layout.activity_truck)
@OptionsMenu(R.menu.login_menu)
public class TruckActivity extends AbstractBaseActivity implements DialogRFIDReadingFragment.ReadingDialogListener {
    DialogRFIDReadingFragment dialog;

    @AfterViews
    protected void init() {
        dialog = new DialogRFIDReadingFragment();
        dialog.init(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mApplication.getHandler().setOnEventListener(null);
    }

    public void onClickReadTag(View view) {
        dialog.startReading();
    }

    public Tag getTagFromBytes(byte[] tag) throws InvalidTagException
    {
        return new TruckTag(tag);
    }

    public void onReadingNew(Tag tag) {
        final TruckTag truck = (TruckTag) tag;

        try {
            mApplication.api.getTruck(truck, new ThingpleAPI.Listener<TruckModel>() {
                @Override
                public void onResponse(TruckModel t) {
                    t.save();
                    Log.i("TruckActivity", "Truck retrieved! (" + t.getPlateNumber() + ")");
                    //Starting a new Intent
                    truck.setTruck(t);
                    Intent nextScreen = new Intent(getApplicationContext(), CustomerActivity_.class);
                    nextScreen.putExtra("truck", truck);
                    startActivity(nextScreen);
                    TruckActivity.this.finish();
                }

                @Override
                public void onError(String message) {
                    Log.i("TruckActivity", "Error: " + message);
                    TruckModel t = TruckModel.getTruck(truck);
                    if(t == null)
                    {
                        showToast(message);
                        return;
                    }
                    Log.i("TruckActivity", "Truck retrieved from cache! (" + t.getPlateNumber() + ")");
                    //Starting a new Intent
                    truck.setTruck(t);
                    Intent nextScreen = new Intent(getApplicationContext(), CustomerActivity_.class);
                    nextScreen.putExtra("truck", truck);
                    startActivity(nextScreen);
                    TruckActivity.this.finish();
                }
            });
        } catch(APICallException e)
        {
            e.printStackTrace();
            Log.e("TruckActivity", e.getMessage());
            showToast(e.getMessage());
        }
    }
    /*
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent a = new Intent(this,TruckActivity_.class);
            a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(a);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    */
}
